package com.yanqu.road.server.cmd.activity.jiuzhou;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_JIU_ZHOU_GET_ROOM_CHAT, desc = "获取直播间")
public class JiuZhouGetRoomChatCmd extends AbstractCommand {

    private int activityId;

    private int roomId;

    private long chatUserId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        JiuZhouProto.JiuZhouGetRoomChatReqMsg reqMsg = JiuZhouProto.JiuZhouGetRoomChatReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        roomId = reqMsg.getRoomId();
        chatUserId = reqMsg.getChatUserId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
            return;
        }
        if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_JIU_ZHOU_ACTIVITY_NOT_IN_TIME;
            return;
        }
        if (config.getActivityInfo().getActivityId() != activityId) {
            ret = GameErrorCode.E_JIU_ZHOU_ACTIVITY_NOT_IN_TIME;
            return;
        }

        // 上个跨服
        CrossJiuZhouProto.CrossJiuZhouGetRoomChatReqMsg.Builder builder = CrossJiuZhouProto.CrossJiuZhouGetRoomChatReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setRoomId(roomId);
        builder.setChatUserId(chatUserId);
        player.sendPacket(Protocol.C_CROSS_JIU_ZHOU_GET_ROOM_CHAT, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            JiuZhouProto.JiuZhouGetRoomChatRespMsg.Builder respMsg =  JiuZhouProto.JiuZhouGetRoomChatRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_GET_ROOM_CHAT, respMsg);
        }
    }
}
